/* 
创建store的过程
1. 引进createStore
2. 调用createStore，传入 reducer
3. 定义初始值
4. 把创建好的 store 导出去

5. 封装创建 action 对象的函数

6. 逻辑中判断 action 类型，返回新的state
*/

import { legacy_createStore as createStore } from 'redux';

const initState = { count: 1000 };
const countReducer = (state = initState, action) => {
  console.log('action -----> ', action);
  switch (action.type) {
    case 'add':
      return { ...state, count: state.count + action.payload };
    case 'dec':
      return { ...state, count: state.count - action.payload };
    default:
      return state;
  }
};
const store = createStore(countReducer);
// 加1
export const addAction = (payload) => {
  return { type: 'add', payload };
};
// 减1
export const decAction = (payload) => {
  return { type: 'dec', payload };
};
export default store;
